
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>用户管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">资源列表</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row">
            <div class="layui-form" style="float:right;">
              <div class="layui-form-item" style="margin:0;">
                <div class="layui-input-inline">
                  <input type="text" name="title" id="title" placeholder="资源标题" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <select name="system" id="system" lay-search="">
                    <option value="-99">所属系统</option>
                    <option value="1">管理员面板</option>
                    <option value="2">商户系统</option>
                    <option value="3">代理商系统</option>
                  </select>
                </div>
                <div class="layui-input-inline">
                  <select name="type" id="status" lay-search="">
                    <option value="-99">状态</option>
                    <option value="1">启用</option>
                    <option value="0">停止</option>
                  </select>
                </div>
                <button id="search" class="layui-btn" data-type="reload">搜索</button>
              </div>
            </div>
          </div>
          <div class="layui-row">
            <div class="layuiAdmin-btns" style="margin-bottom: 10px;">
              <a class="layui-btn" lay-href="sys/resource/resource_add">新增</a>
              <button class="layui-btn" data-type="all" data-events="del">删除</button>
            </div>
          </div>
          <table id="JeePay_Mgr_Resource_dataAll" lay-filter="JeePay_Mgr_Resource_dataAll"></table>
        </div>

      </div>
    </div>
  </div>
</div>

<script type="text/html" id="jeepayBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-xs" lay-event="add">添加子资源</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
layui.use(['admin', 'table', 'util'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,table = layui.table
  ,element = layui.element
  ,form = layui.form;
  
  element.render('breadcrumb', 'breadcrumb');

  var tplSystem = function(d){
    if(d.system == 1) {
      return "<span style='color: green'>管理员面板</span>";
    }else if(d.system == 2) {
      return "<span style='color: blue'>商户系统</span>";
    }else if(d.system == 3) {
      return "<span style='color: blue'>代理商系统</span>";
    }
  };

  //资源列表
  table.render({
    elem: '#JeePay_Mgr_Resource_dataAll'
    ,url: layui.setter.baseUrl + '/sys/resource/list' //列表接口
    ,where: {
      access_token: layui.data(layui.setter.tableName).access_token
    }
    ,id: 'tableReload'
    ,page: true
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'resourceId', title: '资源ID'}
      ,{field: 'name', title: '资源名称'}
      ,{field: 'title', title: '资源标题'}
      ,{field: 'permName', title: '授权名'}
      ,{field: 'permUrl', title: '授权URL'}
      ,{field: 'system', title: '所属系统', templet: tplSystem}
      ,{field: 'edit', width:260, title: '操作', toolbar: '#jeepayBar' }
    ]]
    ,skin: 'line'
  });

  //监听工具条
  table.on('tool(JeePay_Mgr_Resource_dataAll)', function(obj){
    var data = obj.data;
    if(obj.event === 'detail'){
      location.hash = "/sys/resource/resource_view/resourceId=" + data.resourceId;
    } else if(obj.event === 'del'){
      layer.confirm('确认删除么', function(index){
        admin.req({
          type: 'get',
          url: layui.setter.baseUrl + '/sys/resource/delete',
          data: {
            resourceIds: data.resourceId,
          },
          success: function(res){
            if(res.code == 0) {
              layer.msg('删除成功', {icon: 1});
              table.reload("tableReload");
            }
          }
        });
      });
    } else if(obj.event === 'edit'){
      location.hash = "/sys/resource/resource_edit/resourceId=" + data.resourceId;
    } else if(obj.event === 'add'){
      location.hash = "/sys/resource/resource_add/resourceId=" + data.resourceId;
    }
  });


  // 搜索
  var $ = layui.$, active = {
    reload: function(){
      var title = $("#title").val();
      var system = $("#system").val();
      var status = $("#status").val();
      //执行重载
      table.reload('tableReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          title: title,
          system : system,
          status : status
        }
      });
    }
  };
  $('#search').on('click', function() {
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

  // 事件处理
  var events = {
    del: function(othis, type){
      var checkStatus = table.checkStatus("tableReload")
      ,data = checkStatus.data; //获得选中的数据
      if(data.length === 0) return layer.msg('请选择数据');
      console.log(data);
      layer.confirm('确定删除选中的数据吗？', function(){
        var resourceIds = "";
        $.each(data, function(i, n){
          resourceIds = resourceIds + "," + n.resourceId;
        });
        console.log("userIds");
        if(resourceIds != "") {
          resourceIds = resourceIds.substring(1);
        }
        admin.req({
          type: 'get',
          url: layui.setter.baseUrl + '/sys/resource/delete',
          data: {
            resourceIds: resourceIds,
          },
          success: function(res){
            if(res.code == 0) {
              layer.msg('删除成功', {
                icon: 1
              });
              table.reload("tableReload");
            }
          }
        });
      });
    }
  };
  $('.layuiAdmin-btns .layui-btn').on('click', function(){
    var othis = $(this)
            ,thisEvent = othis.data('events')
            ,type = othis.data('type');
    events[thisEvent] && events[thisEvent].call(this, othis, type);
  });
  // 渲染表单
  form.render();
});
</script>